package com.booksmanager.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.booksmanager.annotation.Log;
import com.booksmanager.pojo.User;
import com.booksmanager.service.UserService;
import com.booksmanager.utils.R;
import com.booksmanager.utils.RequestSession;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author Yan
 * @since 2020-05-24
 */
@Api(value = "/user", tags = "用户表")
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * login
     * @param request
     * @param response
     * @param user
     * @return
     */
    @ApiOperation(value = "登录")
    @Log("登录")
    @PostMapping("/login")
    public R login(HttpServletRequest request, HttpServletResponse response, @RequestBody User user){
        return new R(userService.login(user,request,response));
    }

    /**
     * register
     * @param user
     * @return
     */
    @ApiOperation(value = "注册")
    @Log("注册")
    @PostMapping("/save")
    public R save(@RequestBody User user){
        RequestSession<User> userRequestSession = new RequestSession<>();
        User userSession = userRequestSession.getUser();
        user.setCreadId(userSession.getId());
        user.setCreadName(userSession.getHolder());
        return new R(userService.save(user));
    }

    /**
     *select list by page
     * @param page
     * @param user
     * @return
     */
    @ApiOperation(value = "分页查询角色")
    @GetMapping("/page")
    public R<Page> page(Page page,User user){
        return new R(userService.queryPageList(page,user));
    }

    /**
     * select by id
     * @param id
     * @return
     */
    @ApiOperation(value = "根据id查询")
    @GetMapping("/queryById")
    public R<User> queryById(@RequestParam("id") String id){
        return new R(userService.getById(id));

    }

    /**
     * update user by id
     * @param user
     * @return
     */
    @ApiOperation(value = "更新用户")
    @Log("更新用户")
    @PutMapping("/update")
    public R update(@RequestBody User user){
        return new R(userService.updateById(user));
    }

    /**
     * 逻辑删除
     * @param user
     * @return
     */
    @ApiOperation(value = "逻辑删除角色")
    @Log("逻辑删除角色")
    @DeleteMapping("/delete")
    public R delete(@RequestBody User user){
        user.setDelFlag("-1");
        return new R(userService.updateById(user));
    }

}

